<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
    	$this->login_verify();
        $banner=M('banner');
        $bannerlist=$banner->select();
        $this->assign("bannerlist",$bannerlist);

        $carousel=M('carousel');
        $carousellist=$carousel->select();
        $this->assign("carousellist",$carousellist);	
        $this->display();
    }
    public function bannerSet(){
    	// var_dump($_FILES);
	    $upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     2097152 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->saveName = 'time';
	    $upload->subName = 'banner';
	    $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
	    // 上传单个文件 
	    $info   =   $upload->uploadOne($_FILES['banner']);
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功 获取上传文件信息
	        $banner=M('banner');
	        $data['imgurl']='Uploads/'.$info['savepath'].$info['savename'];
	        if($banner->data($data)->add()){
	        	$this->success("上传成功",U('Index/index',array("type"=>1)));
	        }else{
	        	$this->error("上传失败");
	        }
	    }
    }

    // 轮播
    public function carousel_display(){
    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     2097152 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->saveName = 'time';
	    $upload->subName = 'carousel';
	    $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
	    // 上传单个文件 
	    $info   =   $upload->uploadOne($_FILES['carousel']);
	    if(!$info) {// 上传错误提示错误信息
	        $this->error($upload->getError());
	    }else{// 上传成功 获取上传文件信息
	        $carousel=M('carousel');
	        $data['imgurl']='Uploads/'.$info['savepath'].$info['savename'];
	        if($carousel->data($data)->add()){
	        	$this->success("上传成功",U('Index/index',array("type"=>2)));
	        }else{
	        	$this->error("上传失败");
	        }
	    }
	}

    public function bannerDel(){
    	$id=I('get.id');
    	$banner=M('banner');
    	if($banner->where('id='.$id)->delete()){
	        	$this->success("删除成功",U('Index/index',array("type"=>1)));
    	}else{
	        	$this->error("删除失败");
    	}
    }
    public function carouselDel(){
    	$id=I('get.id');
    	$carousel=M('carousel');
    	if($carousel->where('id='.$id)->delete()){
	        	$this->success("删除成功",U('Index/index',array("type"=>2)));
    	}else{
	        	$this->error("删除失败");
    	}
    }
}